//
//  L_StartVC.swift
//  letuofu
//
//  Created by dooland2 on 2020/2/20.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit
import SDWebImage

class L_StartVC: UIViewController {
    var imgs = [UIImageView]()
    let mainScrView = UIScrollView()
    var guideFinished: (() -> Void)? = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置应用程序右上角的提醒个数
        UIApplication.shared.applicationIconBadgeNumber = 0

        // Do any additional setup after loading the view.
        getSysguidepage(callback: { (suc,imgs) in
            for img in imgs ?? [] {
//                SDWebImageManager.shared.loadImage(with: URL(string: strurl), options: [], progress: nil, completed: {image,_,_,_,_,_ in
//                    SDImageCache.shared.store(image, forKey: strurl, completion: nil)
//                })
                
                let imgUrl = img.getString("url")
                let imgview = UIImageView()
                let url = URL(string: imgUrl?.replacingOccurrences(of: "http://120.84.140.138:26666", with: "http://121.40.30.187:8080") ?? "")
                imgview.sd_setImage(with: url)
                imgview.contentMode = .scaleAspectFit
                self.imgs.append(imgview)
            }
            
            self.setupUI()
        })
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

    func getSysguidepage(callback: @escaping (Bool,[[String: Any?]]?) -> Void) {
        API.formRequest(url: .YI_GetSysguidepage, params: nil, callback: { (result) in
        if result.success {
            if let dl = result.data?.getDictionary("data") {
                let dll = dl.getString("imgs")
                let data = dll?.data(using: String.Encoding.utf8)
                
                do {
                    let imgs = try JSONSerialization.jsonObject(with: data ?? Data(), options: JSONSerialization.ReadingOptions.fragmentsAllowed)
                    callback(true, imgs as? [[String : Any?]])

                } catch {

                }
 
           }
           else {
               callback(false,nil)
            }
        }else{
            callback(false,nil)
        }
            
        })
    }
}


// MARK:- 设置UI界面
extension L_StartVC {
    func setupUI() {
        self.view.drawView(view: mainScrView) { (make) in
            make.top.bottom.left.right.equalToSuperview()
        }
        mainScrView.isPagingEnabled = true
        mainScrView.isScrollEnabled = true
        mainScrView.backgroundColor = .white

        let w = REAL_SCREEN_WIDTH*CGFloat(imgs.count)
        if imgs.count > 0 {
            for i in 0..<imgs.count {
                mainScrView.drawImage(imageView: imgs[i], named: nil) { (make) in
                    make.height.equalTo(REAL_SCREEN_HEIGHT)
                    make.width.equalTo(REAL_SCREEN_WIDTH)
                    make.left.equalTo(CGFloat(i)*REAL_SCREEN_WIDTH)
                }
                if i+1 == imgs.count {
                    imgs[i].addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(qingchuhuancunTap(tap:))))
                    imgs[i].isUserInteractionEnabled = true
                }
            }
            mainScrView.contentSize = .init(width: w, height: REAL_SCREEN_HEIGHT)
        }
        else {
            let imgview = mainScrView.drawImage(named: "yindao") { (make) in
                make.top.left.height.width.equalToSuperview()
            }
            imgview.contentMode = .scaleAspectFit
            mainScrView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(qingchuhuancunTap(tap:))))
        }
        

//        let popup = PopupConfirmImgBox(imgname: "yinsizhengce")
//        popup.show(vc: UIViewController.topMostController() ?? self)
//        popup.onSureButtonClick = {
////            LocalStorage.saveBool(true, forKey: YINSI_KEY)
//            LocalStorage.saveBool(true, forKey: YINDAO_KEY)
//        }
//        popup.onTapImgClick = {
//            print("open img")
//
//            let vc = M_ArticleVC()
//            self.present(vc, animated: true, completion: nil)
//            vc.headTitle.text = "隐私政策"
//            vc.backButton.isHidden = true
//            vc.loadUrl("http://www.qiuzhangwaimai.com/leTuo_H5/agreement1.html")
//            vc.backBtn.isHidden = false
//        }
//        popup.onCancelButtonClick = {
//            abort()
//        }
    }
}

// MARK:- 事件
extension L_StartVC {
    @objc func qingchuhuancunTap(tap:UITapGestureRecognizer){
        let nav = PushAndPopNavigationController()
        nav.isNavigationBarHidden = true
        let vc = L_LoginVC()
        nav.setViewControllers([vc], animated: false)
        UIApplication.shared.keyWindow!.rootViewController = nav
        self.dismiss(animated: false, completion: {
        })
    }
    @objc func qingchuhuancunClick(btn: Button){
        self.dismiss(animated: true, completion: {
            if let pop = self.guideFinished {
                pop()
            }
        })
    }
}
